package main

import (
	"encoding/json"
	"first/migrate"
	"first/service"
	"github.com/gin-gonic/gin"
	"github.com/thinkerou/favicon"
	"gorm.io/gorm"
	"log"
	"net/http"
)

// 自定义中间件 拦截器
func myHandler() (gin.HandlerFunc) {
	return func(context *gin.Context) {
		context.Set("user_session", "userid-66666666666666")
		// 放行
		context.Next()
		// 阻断
		// context.Abort()
	}
}

var db *gorm.DB

func main() {
	ginServer := gin.Default()
	// 注册中间件
	ginServer.Use(myHandler())

	ginServer.LoadHTMLGlob("./templates/*")
	ginServer.Static("/static", "./static")

	ginServer.Use(favicon.New("./static/img/favicon.png"))

	// http://localhost:8082/hello
	ginServer.GET("/hello", func(context *gin.Context) {
		user_session := context.MustGet("user_session").(string)
		log.Println("获取的session:", user_session)
		context.JSON(200, gin.H{
			"code": 200,
			"msg": "hello world!",
		})
	})

	// http://localhost:8082/index
	ginServer.GET("/index", func(context *gin.Context) {
		context.HTML(200, "index.html", gin.H{
			"msg": "后端的数据,你好，go!",
		})
	})

	// http://localhost:8082/user/info?userid=1001&username=frank
	ginServer.GET("/user/info", func(context *gin.Context) {
		user_session := context.MustGet("user_session").(string)
		log.Println("获取的session:", user_session)
		userid := context.Query("userid")
		username := context.Query("username")
		context.JSON(200, gin.H{
			"userid": userid,
			"username": username,
			"user_session": user_session,
		})
	})

	// http://localhost:8082/user/1001/frank
	ginServer.GET("/user/:userid/:username", func(context *gin.Context) {
		userid := context.Param("userid")
		username := context.Param("username")
		context.JSON(200, gin.H{
			"userid": userid,
			"username": username,
		})
	})

	ginServer.POST("/json", func(context *gin.Context) {
		data, _ := context.GetRawData()
		var m map[string]interface{}
		_ = json.Unmarshal(data, &m)
		context.JSON(http.StatusOK, m)
	})

	// 表单保存
	ginServer.POST("/user", func(context *gin.Context) {
		username := context.PostForm("username")
		password := context.PostForm("password")
		context.JSON(200, gin.H{
			"password": password,
			"username": username,
		})
	})

	ginServer.PUT("/user", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"msg": "修改的界面",
		})
	})

	ginServer.DELETE("/user", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"msg": "删除的界面",
		})
	})

	// 重定向
	ginServer.GET("/test", func(context *gin.Context) {
		context.Redirect(http.StatusMovedPermanently, "https://www.baidu.com")
	})

	// 404
	ginServer.NoRoute(func(context *gin.Context) {
		context.HTML(http.StatusNotFound, "404.html", nil)
	})

	// 路由组
	userGroup := ginServer.Group("/user")
	{
		userGroup.GET("/add")
		userGroup.GET("/login")
		userGroup.GET("/logout")
	}

	orderGroup := ginServer.Group("/order")
	{
		orderGroup.GET("/add")
		orderGroup.GET("/delete")
		orderGroup.GET("/update")
	}

	// 操作数据库
	db = migrate.InitDB()

	// 获取所有用户
	ginServer.GET("/users", service.GetUsers)
	// 保存用户
	ginServer.POST("/user/create", service.CreateUser)
	// http://127.0.0.1:8082/users/frank2 根据名称查询用户信息
	ginServer.GET("/users/:username", func(context *gin.Context) {
		username := context.Param("username")
		user, _ := service.GetUserByUsername(username)
		context.JSON(200, gin.H{
			"data": user,
		})
	})

	ginServer.Run(":8082")
}
